package pjaws;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;

public class RootPawHandler extends Thread {
	public enum Status {
		READY, BUSY
	}

	private Vector<RootPawHandler> pawhandlers;
	private Socket mySocket;
	private DataInputStream in;
	private DataOutputStream out;
	private int myPawId;
	private Root myRoot;

	public RootPawHandler(Socket socket, Vector<RootPawHandler> handlers, Root r)
			throws IOException {
		myRoot = r;
		pawhandlers = handlers;
		mySocket = socket;
		in = new DataInputStream(mySocket.getInputStream());
		out = new DataOutputStream(mySocket.getOutputStream());
	}

	public void run() {		
		try {
			/** **RECEIVE*** */
			// get cmd
			byte[] cmdData = new byte[PjawsConstants.CMDLEN];
			in.read(cmdData);
			String cmd = new String(cmdData);

			// get the paw's id
			System.out.println(new String(cmd));
			if (cmd.equals(PjawsConstants.PAWID)) {
				myPawId = in.readInt();
			}
			System.out.println(myPawId);

			// get the paw's receive status
			in.read(cmdData);
			cmd = new String(cmdData);
			if (cmd.equals(PjawsConstants.STATUS)) {
				int length = in.readInt();
				byte[] status = new byte[length];
				in.read(status);
				myRoot.addPawReceiveStatus(myPawId,RootPawHandler.Status.valueOf(new String(status)));

			}
			System.out.println(new String(cmd));

			// wait till all the paws are ready (ensures socketServers are up)
			while (!myRoot.areServersReadyToReceive())
				;
			System.out.println(myPawId + " is ready to receive");

			// Tell my corresponding paw it can proceed
			out.write(PjawsConstants.STATUS.getBytes());
			out.writeInt(PjawsConstants.PROCEED.length());
			out.write(PjawsConstants.PROCEED.getBytes());

			/** *SEND**** */
			// get cmd
			cmdData = new byte[PjawsConstants.CMDLEN];
			in.read(cmdData);
			cmd = new String(cmdData);

			// get the paw's id
			System.out.println(new String(cmd));
			if (cmd.equals(PjawsConstants.PAWID)) {
				myPawId = in.readInt();
			}
			System.out.println(myPawId);

			// get the paw's send status
			in.read(cmdData);
			cmd = new String(cmdData);
			if (cmd.equals(PjawsConstants.STATUS)) {
				int length = in.readInt();
				byte[] status = new byte[length];
				in.read(status);
				myRoot.addPawSendStatus(myPawId, RootPawHandler.Status.valueOf(new String(status)));

				
			}
			System.out.println(new String(cmd));

			// wait till all the paws are ready to send (ensures sockets are
			// connected)
			while (!myRoot.areServersReadyToSend())
				;
			System.out.println(myPawId + " is ready to send");

			// Tell my corresponding paw it can proceed
			out.write(PjawsConstants.STATUS.getBytes());
			out.writeInt(PjawsConstants.PROCEED.length());
			out.write(PjawsConstants.PROCEED.getBytes());

			in.read(cmdData);
			cmd = new String(cmdData);
			while (!cmd.equals(PjawsConstants.DONE)) {
				in.read(cmdData);
				cmd = new String(cmdData);
			}

		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			try {
				in.close();
				out.close();
				mySocket.close();
			} catch (IOException ioe) {
			} finally {
				synchronized (pawhandlers) {
					pawhandlers.removeElement(this);
				}
			}
		}
	}
}
